package com.cuitrip.util.text;

import android.text.Editable;

/**
 * Created by MiJack on 2015/11/29.
 */
public class CreditCardTextWatcher extends BaseTextWatcher {

    @Override
    public void format(Editable text) {
        int index = -1;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c != ' '&&Character.isDigit(c)) {
                index++;
                //判断是否需要添加空格
                if (index % 4 == 0 && index != 0) {
                    text.insert(i, " ");
                    i++;
                }
//            } else if(!Character.isDigit(c)){
//                //移出空格
//                text.delete(i, i + 1);
//                i--;
            } else {
                //移出空格
                text.delete(i, i + 1);
                i--;
            }
        }
    }
}
